package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.BatchAddAlhProductBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.BatchAddAlhProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 批量添加商品到安利会 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchAddAlhProduct {
	private static BatchAddAlhProductBean batchaddalhproductBean;
	private static BatchAddAlhProductCall batchaddalhproductCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量添加商品到安利会");
	}

	@Before
	public void caseUp() {
		batchaddalhproductBean = new BatchAddAlhProductBean();
		batchaddalhproductCall = new BatchAddAlhProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchaddalhproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public int userId;

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_001() {
		Logger.start(true, "信息正确,添加");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			List<String> list = new ArrayList<String>();
			list.add(productId);
			list.add(productId1);
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			Logger.verifyEquals("200", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_002() {
		Logger.start(true, "未上架商品,添加失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			addProdReq.getProduct().setOnSaleType(2);
			addProdReq.getProduct().setAutoRefresh(false);
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			List<String> list = new ArrayList<String>();
			list.add(productId);
			list.add(productId1);

			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			Logger.verifyEquals("200", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
//			String asString = batchaddalhproductCall.getJsonObject().get("Data").getAsJsonObject().get("ProductMsg")
//					.getAsJsonArray().get(0).getAsJsonObject().get("Msg").getAsString();
//			Logger.verifyEquals("商品已下架无法添加", asString, "无法添加下架商品");

			int size = batchaddalhproductCall.getJsonObject().get("Data").getAsJsonObject().get("ProductMsg")
					.getAsJsonArray().size();
			Logger.verifyEquals(0, size, "无法添加下架商品");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_003() {
		Logger.start(true, "用户id错误,添加失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			List<String> list = new ArrayList<String>();
			list.add(productId);

			batchaddalhproductBean.setUserId(123);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			Logger.verifyEquals("201", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_004() {
		Logger.start(true, "商品id错误  不存在的商品,添加失败");
		try {
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(Arrays.asList("123123123"));
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			Logger.verifyEquals("201", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_005() {
		Logger.start(true, "已经推广中的商品,添加失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			List<String> list = new ArrayList<String>();
			list.add(productId);
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			
			
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			
			Logger.verifyEquals("200", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_006() {
		Logger.start(true, "没有库存的商品，添加失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			
			new YmtCatalogsIWapper().updatestocknumByProductID(0,productId);
			List<String> list = new ArrayList<String>();
			list.add(productId);
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			
			Logger.verifyEquals("200", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchAddAlhProduct_007() {
		Logger.start(true, "商品id列表超过100");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			
			new YmtCatalogsIWapper().updatestocknumByProductID(0,productId);
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < 101; i++) {
				list.add(productId);
			}
			System.out.println(list.size());
			batchaddalhproductBean.setUserId(sellerId);
			batchaddalhproductBean.setProductIds(list);
			batchaddalhproductCall.setData(batchaddalhproductBean);
			batchaddalhproductCall.callService();
			
			Logger.verifyEquals("400", batchaddalhproductCall.getJsonObject().get("Code").getAsString(), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}